package com.inspur.his.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.inspur.his.constant.Constant;
import com.inspur.his.pojo.Dept;
import com.inspur.his.pojo.DeptExt;
import com.inspur.his.pojo.Doct;
import com.inspur.his.pojo.DoctExt;
import com.inspur.his.service.DoctService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
public class DoctorController {
    @Autowired
    DoctService doctService;

    @RequestMapping("doctListController")
    public String doctList(HttpServletRequest request, Model model, @RequestParam(value = "pn", defaultValue = "1") Integer pn) {
        //获取页面提交来的查询条件
        String realname = request.getParameter("realname");
        String departid = request.getParameter("departid");
        //  点击医生管理左侧菜单进行默认无条件查询
        HashMap map = new HashMap();
        map.put("realname", realname);
        map.put("departid", departid);
        PageHelper.startPage(pn, 5);
        List<DoctExt> doctList = doctService.doctList(map);
        PageInfo<DoctExt> pageInfo = new PageInfo<DoctExt>(doctList, doctList.size());
        model.addAttribute("doctlist", doctList);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("realname", realname);
        model.addAttribute("departid", departid);
        return "doct/doctList.jsp";
    }

    @RequestMapping("doctAddController")
    public String doctAdd(Doct doct, Model model) throws Exception {
        Date day = new Date();
        SimpleDateFormat fm = new SimpleDateFormat("yyyy-MM-DD");
        String today = fm.format(day);
        doct.setDocid(today + String.valueOf(day.getTime()));
        doct.setCreatedate(day);
        doct.setStatus(Constant.DOCT_STATUS_ABLE);
        doctService.doctAdd(doct);
        model.addAttribute("doct", doct);
        model.addAttribute("flag", Constant.DOCT_STATUS_ABLE);
        model.addAttribute("success", "医生信息保存成功！");
        return "doct/doctList.jsp";
    }


    @RequestMapping("modifyDoctController")
    public String modifyDoct(String id,Model model) {
        Doct doct = doctService.getDoctById(id);
        model.addAttribute("doct",doct);
        return "doct/doctModify.jsp";
    }
    @RequestMapping("doctUpdateController")
    public String docupdate(Doct doc, Model model) {
        doctService.updateDoct(doc);
        model.addAttribute("success", "医生信息修改成功");
        model.addAttribute("doct", doc);
        model.addAttribute("flag", "1");
        return "doct/doctModify.jsp";
    }
    @RequestMapping("doctchangeStatus")
    public String docchangeStatus(HttpServletRequest req, Model md,
                                  @RequestParam(value = "pn", defaultValue = "1") Integer pn) {
        // 获取页面传递过来的查询条件
        String name = req.getParameter("name");
        String role = req.getParameter("role");
        // 页面查询条件查询后锁定显示在页面上
        md.addAttribute("name", name);
        md.addAttribute("role", role);
        String docid = req.getParameter("docid");
        String sts = req.getParameter("sts");
        HashMap map = new HashMap<String, String>();
        map.put("docid", docid);
        map.put("status", sts);
        doctService.doctchangeStatus(map);
        return "redirect:doctListController";
    }
}
